# -*- python -*-
# ex: set syntax=python:

# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# This is the buildmaster config file for the 'nacl' bot. It must
# be installed as 'master.cfg' in your buildmaster's base directory
# (although the filename can be changed with the --basedir option to
# 'mktap buildbot master').

# It has one job: define a dictionary named BuildmasterConfig. This
# dictionary has a variety of keys to control different aspects of the
# buildmaster. They are documented in docs/config.xhtml .

from master import gitiles_poller
from buildbot.scheduler import Scheduler

from common import chromium_utils

from master import master_utils
from master import slaves_list
from master.factory import nacl_factory

import config
import master_site_config

ActiveMaster = master_site_config.NativeClientToolchain

nacl_source_url = (
    'https://chromium.googlesource.com/native_client/src/native_client')

# This is the dictionary that the buildmaster pays attention to. We also use
# a shorter alias to save typing.
c = BuildmasterConfig = {}

config.DatabaseSetup(c, require_dbconfig=ActiveMaster.is_production_host)

master_poller = gitiles_poller.GitilesPoller(nacl_source_url)

c['change_source'] = [master_poller]

m_nacl_posix = nacl_factory.NativeClientFactory(
    build_dir='native_client', target_platform='linux2',
    alternate_url=nacl_source_url)
m_nacl_win = nacl_factory.NativeClientFactory(
    build_dir='native_client', target_platform='win32',
    alternate_url=nacl_source_url)

# Some shortcut to simplify the code below.
F_NACL = m_nacl_posix.NativeClientFactory
F_NACL_WIN = m_nacl_win.NativeClientFactory


# The identifier of the factory is the build configuration. If two factories
# are using the same build configuration, they should have the same identifier.

# BuilderTesters using a custom build configuration.
factories = [
    ['win7-toolchain_x86', '1native-newlib-x86|info', F_NACL_WIN()],
    ['mac-toolchain_x86', '1native-newlib-x86|info', F_NACL()],
    ['precise64-toolchain_x86', '1native-newlib-x86|info', F_NACL()],

    ['mac-toolchain_arm', '2native-newlib-arm|info', F_NACL()],
    ['precise64-toolchain_arm', '2native-newlib-arm|info', F_NACL()],

    # GlibC makefile
    ['win7-glibc', '3native-glibc-x86|info', F_NACL_WIN()],
    ['mac-glibc', '3native-glibc-x86|info', F_NACL()],
    ['precise64-glibc', '3native-glibc-x86|info', F_NACL()],

    # PNaCl
    ['linux-pnacl-x86_64', '4pnacl|info', F_NACL()],
    ['linux-pnacl-x86_32', '4pnacl|info', F_NACL()],
    ['mac-pnacl-x86_32', '4pnacl|info', F_NACL()],
    ['win-pnacl-x86_32', '4pnacl|info', F_NACL_WIN()],
    ['linux-pnacl-x86_64-tests-x86_32', '5pnacl-fyi|info', F_NACL()],
    ['linux-pnacl-x86_64-tests-x86_64', '5pnacl-fyi|info', F_NACL()],
    ['linux-pnacl-x86_64-tests-arm', '5pnacl-fyi|info', F_NACL()],
    ['linux-pnacl-x86_32-tests-mips', '5pnacl-fyi|info', F_NACL()],

    # Bionic
    ['precise64-toolchain_bionic', '6bionic|info', F_NACL()],

    # Cross
    ['trusty64-toolchain_cross', '7pnacl-cross|info', F_NACL()],

    # Sanitizers
    ['asan', '8sanitizers', F_NACL()],
]

####### SCHEDULERS
## configure the Schedulers
# Main scheduler for all changes in trunk.
primary_builders = []
for f in factories:
  primary_builders.append(f[0])
s_nacl = Scheduler(
   name='nacl',
   branch='master',
   treeStableTimer=60,
   builderNames=primary_builders,
)
c['schedulers'] = [
    s_nacl,
]


# ----------------------------------------------------------------------------
# BUILDER DEFINITIONS

# The 'builders' list defines the Builders. Each one is configured with a
# dictionary, using the following keys:
#  name (required): the name used to describe this bilder
#  slavename (required): which slave to use, must appear in c['slaves']
#  builddir (required): which subdirectory to run the builder in
#  factory (required): a BuildFactory to define how the build is run
#  periodicBuildTime (optional): if set, force a build every N seconds
#  category (optional): it is not used in the normal 'buildbot' meaning. It is
#                       used by gatekeeper to determine which steps it should
#                       look for to close the tree.
#

no_reboots = [
   'win2003-toolchain_x86',
   'win7-glibc',
   'mac-glibc',
   'precise64-glibc',
]

c['builders'] = []
slaves = slaves_list.SlavesList('slaves.cfg', 'NativeClientToolchain')
for f in factories:
  # Don't enable auto_reboot for people testing locally.
  auto_reboot = f[0] not in no_reboots and ActiveMaster.is_production_host
  c['builders'].append({
      'name': f[0],
      'slavenames': slaves.GetSlavesName(builder=f[0]),
      'builddir': f[0],
      'slavebuilddir': 'nacl-toolchain',
      'factory': f[2],
      'category': '%s' % f[1],
      'auto_reboot': auto_reboot,
  })


####### BUILDSLAVES

# The 'slaves' list defines the set of allowable buildslaves. List all the
# slaves registered to a builder. Remove dupes.
c['slaves'] = master_utils.AutoSetupSlaves(c['builders'],
                                           config.Master.GetBotPassword())

# Make sure everything works together.
master_utils.VerifySetup(c, slaves)


####### STATUS TARGETS

# Buildbot master url:
# Must come before AutoSetupMaster().
c['buildbotURL'] = ActiveMaster.buildbot_url

# Adds common status and tools to this master.
master_utils.AutoSetupMaster(
    c, ActiveMaster,
    public_html='../master.chromium/public_html',
    tagComparator=master_poller.comparator,
    templates=['./templates', '../master.client.nacl/templates'])


# Adjust the buildCaches to be 3x the number of slaves per builder.
c['autoBuildCacheRatio'] = 3
